package aries.security.encryption;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES {

    private static String algorithm = "AES";
    private static int iterations = 2;
    private static byte[] key = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    private String text;

    /**
     * Constructor for AES.
     *
     * @param text
     */
    public AES(String text) {
        this.text = text;
    }

    /**
     * Encrypt the text.
     *
     * @param cipherKey
     * @return encrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String encrypt(String cipherKey) throws Exception {
        return encrypt(cipherKey, key, iterations);
    }
    
    /**
     * Encrypt the text.
     *
     * @param cipherKey
     * @param key
     * @return encrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String encrypt(String cipherKey, byte key[]) throws Exception {
        return encrypt(cipherKey, key, iterations);
    }

    /**
     * Encrypt the text.
     *
     * @param cipherKey
     * @param key
     * @param iterations
     * @return encrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String encrypt(String cipherKey, byte key[], int iterations) throws Exception {
        Key theKey = generateKey(key);
        Cipher c = Cipher.getInstance(algorithm);
        c.init(1, theKey);
        String valueToEnc = null;
        String eValue = text;
        for(int i = 0; i < iterations; i++) {
            valueToEnc = (new StringBuilder()).append(cipherKey).append(eValue).toString();
            byte encValue[] = c.doFinal(valueToEnc.getBytes());
            eValue = (new BASE64Encoder()).encode(encValue);
        }
        return eValue;
    }
    
    /**
     * Decrypt the text.
     *
     * @param cipherKey
     * @return decrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String decrypt(String cipherKey) throws Exception {
        return decrypt(cipherKey, key, iterations);
    }

    /**
     * Decrypt the text.
     *
     * @param cipherKey
     * @param key
     * @return decrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String decrypt(String cipherKey, byte key[]) throws Exception {
        return decrypt(cipherKey, key, iterations);
    }

    /**
     * Decrypt the text.
     *
     * @param cipherKey
     * @param key
     * @param iterations
     * @return decrypted text
     * @throws Exception
     * @deprecated Use Sun propiertary.
     */
    public String decrypt(String cipherKey, byte key[], int iterations) throws Exception {
        Key theKey = generateKey(key);
        Cipher c = Cipher.getInstance(algorithm);
        c.init(2, theKey);
        String dValue = null;
        String valueToDecrypt = text;
        for(int i = 0; i < iterations; i++) {
            byte decordedValue[] = (new BASE64Decoder()).decodeBuffer(valueToDecrypt);
            byte decValue[] = c.doFinal(decordedValue);
            dValue = (new String(decValue)).substring(cipherKey.length());
            valueToDecrypt = dValue;
        }
        return dValue;
    }

    /**
     * Create a secret key.
     *
     * @param key
     * @return secret key
     * @throws Exception
     */
    private Key generateKey(byte key[]) throws Exception {
        Key theKey = new SecretKeySpec(key, algorithm);
        return theKey;
    }

}